package com.my.study.aop.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/11/5 9:01
 **/
public class JDKProxy implements InvocationHandler {

    private Object target;

    public JDKProxy(Object target) {
        this.target = target;
    }

    public Object getTarget() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    /**
     * 增强逻辑实现方法
     *
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("执行 " + method.getName() + "...");

        Object invoke = method.invoke(target, args);
//        String name = method.getName();
//
//        if ("add".equals(name)) {
//            System.out.println("执行add()...");
//            return method.invoke(proxy, args);
//        }
//
//        if ("update".equals(name)) {
//            System.out.println("执行update()...");
//            return method.invoke(proxy, args);
//        }


        return invoke;
    }
}
